﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Streams;

namespace Wazzup.Engine.Utility
{
    public static class FileHelper
    {
        static public async Task<StorageFile> IsFileExist(string name)
        {
            string folderName = "";
            string fileName = name;
            if (name.Contains("\\"))
            {
                string[] p = StringHelper.Split(name, '\\');

                folderName = p[0];
                fileName = p[1];
            }

            StorageFolder localFolder = ApplicationData.Current.LocalFolder;

            if (!string.IsNullOrEmpty(folderName))
                return await IsFileExist(folderName, fileName);
            else
                return await IsFileExist(localFolder, name);
        }

        static public async Task<StorageFile> IsFileExist(string folderPath, string name)
        {
            StorageFolder folder = null;
            if (folderPath.Contains("\\"))
            {
                string[] p  = StringHelper.Split(folderPath, '\\');

                folder = ApplicationData.Current.LocalFolder;
                foreach (var f in p)
                {
                    StorageFolder currentFolder = null;
                    try
                    {
                        currentFolder = await folder.GetFolderAsync(f);
                    }
                    catch
                    {
                    }

                    if (currentFolder == null)
                        return null;

                    folder = currentFolder;
                }
            }
            else
            {
                folder = await IsFolderExist(folderPath);
                if (folder == null)
                    return null;
            }

            return await IsFileExist(folder, name);
        }

        static public async Task<StorageFile> IsFileExist(StorageFolder folder, string name)
        {
            StorageFile file = null;
            try
            {
                file = await folder.GetFileAsync(name);
            }
            catch { }

            return file;
        }
        static public async Task<StorageFile> IsFileExistCheck(StorageFolder folder, string name)
        {
            StorageFile file = null;
            try
            {
                file = await folder.GetFileAsync(name);
                BasicProperties properties = await file.GetBasicPropertiesAsync();
                if (properties.Size == 0)
                {
                    return null;
                }
            }
            catch { }

            return file;
        }
        static public async Task<StorageFolder> IsFolderExist(string name)
        {
            StorageFolder localFolder = ApplicationData.Current.LocalFolder;

            StorageFolder folder = null;
            try
            {
                folder = await localFolder.GetFolderAsync(name);
            }
            catch
            {
            }

            return folder;
        }

        public static async Task<byte[]> CreateImageThumbnail(StorageFile s, string name, string folder, ThumbnailMode mode)
        {
            StorageItemThumbnail thumbnail = await s.GetThumbnailAsync(mode);

            if (thumbnail == null)
                return null;

            bool isNew = false;
            StorageFile newFile = await CreateLocalFile(name, folder);

            using (DataReader reader = new DataReader(thumbnail))
            {
                await reader.LoadAsync((uint)thumbnail.Size);
                IBuffer buffer = reader.ReadBuffer((uint)thumbnail.Size);

                await FileIO.WriteBufferAsync(newFile, buffer);
            }

            var fStream = await newFile.OpenAsync(FileAccessMode.Read);

            var readerByte = new DataReader(fStream.GetInputStreamAt(0));
            var bytes = new byte[fStream.Size];
            await readerByte.LoadAsync((uint)fStream.Size);
            readerByte.ReadBytes(bytes);
            return bytes;
        }

        public static async Task<StorageFile> CreateLocalFile(string fileName, string folderPath, bool replaceExisting = false)
        {
            StorageFolder localFolder = ApplicationData.Current.LocalFolder;
            StorageFolder folder = null;

            if (folderPath.Contains("\\"))
            {
                string[] p = StringHelper.Split(folderPath, '\\');

                folder = localFolder;
                foreach (var f in p)
                {
                    StorageFolder currentFolder = null;
                    try
                    {
                        currentFolder = await folder.GetFolderAsync(f);
                    }
                    catch
                    {
                    }

                    if (currentFolder == null)
                        folder = await folder.CreateFolderAsync(f, CreationCollisionOption.OpenIfExists);
                    else
                        folder = currentFolder;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(folderPath))
                    folder = localFolder;
                else
                {
                    folder = await IsFolderExist(folderPath);
                    if (folder == null)
                        folder = await localFolder.CreateFolderAsync(folderPath, CreationCollisionOption.OpenIfExists);
                }
            }

            StorageFile file = null;
            file = await IsFileExist(folder, fileName);

            if (replaceExisting && file != null)
            {
                await file.DeleteAsync();
                file = null;
            }

            if (file == null)
                file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);

            return file;
        }

        public async static Task<byte[]> GetByteArrayFile(string path)
        {
            StorageFile videoFile = await IsFileExist(path);
            Stream videoFileStream = await videoFile.OpenStreamForReadAsync();
            Byte[] videoArray = new Byte[videoFileStream.Length];
            await videoFileStream.ReadAsync(videoArray, 0, Convert.ToInt32(videoFileStream.Length));
            return videoArray;
        }
    }
}
